<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Debugger | Node.js v8.12.0 Documentation</title>
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic">
  <link rel="stylesheet" href="assets/style.css">
  <link rel="stylesheet" href="assets/sh.css">
  <link rel="canonical" href="https://nodejs.org/api/debugger.html">
</head>
<body class="alt apidoc" id="api-section-debugger">
  <div id="content" class="clearfix">
    <div id="column2" class="interior">
      <div id="intro" class="interior">
        <a href="/" title="Go back to the home page">
          Node.js
        </a>
      </div>
      <ul>
<li><a class="nav-documentation" href="documentation.html">About these Docs</a></li>
<li><a class="nav-synopsis" href="synopsis.html">Usage &amp; Example</a></li>
</ul>
<div class="line"></div>

<ul>
<li><a class="nav-assert" href="assert.html">Assertion Testing</a></li>
<li><a class="nav-async_hooks" href="async_hooks.html">Async Hooks</a></li>
<li><a class="nav-buffer" href="buffer.html">Buffer</a></li>
<li><a class="nav-addons" href="addons.html">C++ Addons</a></li>
<li><a class="nav-n-api" href="n-api.html">C/C++ Addons - N-API</a></li>
<li><a class="nav-child_process" href="child_process.html">Child Processes</a></li>
<li><a class="nav-cluster" href="cluster.html">Cluster</a></li>
<li><a class="nav-cli" href="cli.html">Command Line Options</a></li>
<li><a class="nav-console" href="console.html">Console</a></li>
<li><a class="nav-crypto" href="crypto.html">Crypto</a></li>
<li><a class="nav-debugger active" href="debugger.html">Debugger</a></li>
<li><a class="nav-deprecations" href="deprecations.html">Deprecated APIs</a></li>
<li><a class="nav-dns" href="dns.html">DNS</a></li>
<li><a class="nav-domain" href="domain.html">Domain</a></li>
<li><a class="nav-esm" href="esm.html">ECMAScript Modules</a></li>
<li><a class="nav-errors" href="errors.html">Errors</a></li>
<li><a class="nav-events" href="events.html">Events</a></li>
<li><a class="nav-fs" href="fs.html">File System</a></li>
<li><a class="nav-globals" href="globals.html">Globals</a></li>
<li><a class="nav-http" href="http.html">HTTP</a></li>
<li><a class="nav-http2" href="http2.html">HTTP/2</a></li>
<li><a class="nav-https" href="https.html">HTTPS</a></li>
<li><a class="nav-inspector" href="inspector.html">Inspector</a></li>
<li><a class="nav-intl" href="intl.html">Internationalization</a></li>
<li><a class="nav-modules" href="modules.html">Modules</a></li>
<li><a class="nav-net" href="net.html">Net</a></li>
<li><a class="nav-os" href="os.html">OS</a></li>
<li><a class="nav-path" href="path.html">Path</a></li>
<li><a class="nav-perf_hooks" href="perf_hooks.html">Performance Hooks</a></li>
<li><a class="nav-process" href="process.html">Process</a></li>
<li><a class="nav-punycode" href="punycode.html">Punycode</a></li>
<li><a class="nav-querystring" href="querystring.html">Query Strings</a></li>
<li><a class="nav-readline" href="readline.html">Readline</a></li>
<li><a class="nav-repl" href="repl.html">REPL</a></li>
<li><a class="nav-stream" href="stream.html">Stream</a></li>
<li><a class="nav-string_decoder" href="string_decoder.html">String Decoder</a></li>
<li><a class="nav-timers" href="timers.html">Timers</a></li>
<li><a class="nav-tls" href="tls.html">TLS/SSL</a></li>
<li><a class="nav-tracing" href="tracing.html">Tracing</a></li>
<li><a class="nav-tty" href="tty.html">TTY</a></li>
<li><a class="nav-dgram" href="dgram.html">UDP/Datagram</a></li>
<li><a class="nav-url" href="url.html">URL</a></li>
<li><a class="nav-util" href="util.html">Utilities</a></li>
<li><a class="nav-v8" href="v8.html">V8</a></li>
<li><a class="nav-vm" href="vm.html">VM</a></li>
<li><a class="nav-zlib" href="zlib.html">ZLIB</a></li>
</ul>
<div class="line"></div>

<ul>
<li><a class="nav-https-github-com-nodejs-node" href="https://github.com/nodejs/node">GitHub Repo &amp; Issue Tracker</a></li>
<li><a class="nav-https-groups-google-com-group-nodejs" href="https://groups.google.com/group/nodejs">Mailing List</a></li>
</ul>

    </div>

    <div id="column1" data-id="debugger" class="interior">
      <header>
        <h1>Node.js v8.12.0 Documentation</h1>
        <div id="gtoc">
          <ul>
            <li>
              <a href="index.html" name="toc">Index</a>
            </li>
            <li>
              <a href="all.html">View on single page</a>
            </li>
            <li>
              <a href="debugger.json">View as JSON</a>
            </li>
            
    <li class="version-picker">
      <a href="#">View another version <span>&#x25bc;</span></a>
      <ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v9.x/api/debugger.html">9.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v8.x/api/debugger.html">8.x <b>LTS</b></a></li>
<li><a href="https://nodejs.org/docs/latest-v7.x/api/debugger.html">7.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v6.x/api/debugger.html">6.x <b>LTS</b></a></li>
<li><a href="https://nodejs.org/docs/latest-v5.x/api/debugger.html">5.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v4.x/api/debugger.html">4.x <b>LTS</b></a></li>
<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/debugger.html">0.12.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/debugger.html">0.10.x</a></li></ol>
    </li>
  
          </ul>
        </div>
        <hr>
      </header>

      <div id="toc">
        <h2>Table of Contents</h2>
        <ul>
<li><span class="stability_2"><a href="#debugger_debugger">Debugger</a></span><ul>
<li><span class="stability_undefined"><a href="#debugger_watchers">Watchers</a></span></li>
<li><span class="stability_undefined"><a href="#debugger_command_reference">Command reference</a></span><ul>
<li><span class="stability_undefined"><a href="#debugger_stepping">Stepping</a></span></li>
<li><span class="stability_undefined"><a href="#debugger_breakpoints">Breakpoints</a></span></li>
<li><span class="stability_undefined"><a href="#debugger_information">Information</a></span></li>
<li><span class="stability_undefined"><a href="#debugger_execution_control">Execution control</a></span></li>
<li><span class="stability_undefined"><a href="#debugger_various">Various</a></span></li>
</ul>
</li>
<li><span class="stability_undefined"><a href="#debugger_advanced_usage">Advanced Usage</a></span><ul>
<li><span class="stability_undefined"><a href="#debugger_v8_inspector_integration_for_node_js">V8 Inspector Integration for Node.js</a></span></li>
</ul>
</li>
</ul>
</li>
</ul>

      </div>

      <div id="apicontent">
        <h1>Debugger<span><a class="mark" href="#debugger_debugger" id="debugger_debugger">#</a></span></h1>
<!--introduced_in=v0.9.12-->
<div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><!-- type=misc -->
<p>Node.js includes an out-of-process debugging utility accessible via a
<a href="#debugger_v8_inspector_integration_for_node_js">V8 Inspector</a> and built-in debugging client. To use it, start Node.js
with the <code>inspect</code> argument followed by the path to the script to debug; a prompt
will be displayed indicating successful launch of the debugger:</p>
<pre><code class="lang-txt">$ node inspect myscript.js
&lt; Debugger listening on ws://127.0.0.1:9229/80e7a814-7cd3-49fb-921a-2e02228cd5ba
&lt; For help see https://nodejs.org/en/docs/inspector
&lt; Debugger attached.
Break on start in myscript.js:1
&gt; 1 (function (exports, require, module, __filename, __dirname) { global.x = 5;
  2 setTimeout(() =&gt; {
  3   console.log(&#39;world&#39;);
debug&gt;
</code></pre>
<p>Node.js&#39;s debugger client is not a full-featured debugger, but simple step and
inspection are possible.</p>
<p>Inserting the statement <code>debugger;</code> into the source code of a script will
enable a breakpoint at that position in the code:</p>
<!-- eslint-disable no-debugger -->
<pre><code class="lang-js">// myscript.js
global.x = 5;
setTimeout(() =&gt; {
  debugger;
  console.log(&#39;world&#39;);
}, 1000);
console.log(&#39;hello&#39;);
</code></pre>
<p>Once the debugger is run, a breakpoint will occur at line 3:</p>
<pre><code class="lang-txt">$ node inspect myscript.js
&lt; Debugger listening on ws://127.0.0.1:9229/80e7a814-7cd3-49fb-921a-2e02228cd5ba
&lt; For help see https://nodejs.org/en/docs/inspector
&lt; Debugger attached.
Break on start in myscript.js:1
&gt; 1 (function (exports, require, module, __filename, __dirname) { global.x = 5;
  2 setTimeout(() =&gt; {
  3   debugger;
debug&gt; cont
&lt; hello
break in myscript.js:3
  1 (function (exports, require, module, __filename, __dirname) { global.x = 5;
  2 setTimeout(() =&gt; {
&gt; 3   debugger;
  4   console.log(&#39;world&#39;);
  5 }, 1000);
debug&gt; next
break in myscript.js:4
  2 setTimeout(() =&gt; {
  3   debugger;
&gt; 4   console.log(&#39;world&#39;);
  5 }, 1000);
  6 console.log(&#39;hello&#39;);
debug&gt; repl
Press Ctrl + C to leave debug repl
&gt; x
5
&gt; 2 + 2
4
debug&gt; next
&lt; world
break in myscript.js:5
  3   debugger;
  4   console.log(&#39;world&#39;);
&gt; 5 }, 1000);
  6 console.log(&#39;hello&#39;);
  7
debug&gt; .exit
</code></pre>
<p>The <code>repl</code> command allows code to be evaluated remotely. The <code>next</code> command
steps to the next line. Type <code>help</code> to see what other commands are available.</p>
<p>Pressing <code>enter</code> without typing a command will repeat the previous debugger
command.</p>
<h2>Watchers<span><a class="mark" href="#debugger_watchers" id="debugger_watchers">#</a></span></h2>
<p>It is possible to watch expression and variable values while debugging. On
every breakpoint, each expression from the watchers list will be evaluated
in the current context and displayed immediately before the breakpoint&#39;s
source code listing.</p>
<p>To begin watching an expression, type <code>watch(&#39;my_expression&#39;)</code>. The command
<code>watchers</code> will print the active watchers. To remove a watcher, type
<code>unwatch(&#39;my_expression&#39;)</code>.</p>
<h2>Command reference<span><a class="mark" href="#debugger_command_reference" id="debugger_command_reference">#</a></span></h2>
<h3>Stepping<span><a class="mark" href="#debugger_stepping" id="debugger_stepping">#</a></span></h3>
<div class="signature"><ul>
<li><code>cont</code>, <code>c</code> - Continue execution</li>
<li><code>next</code>, <code>n</code> - Step next</li>
<li><code>step</code>, <code>s</code> - Step in</li>
<li><code>out</code>, <code>o</code> - Step out</li>
<li><code>pause</code> - Pause running code (like pause button in Developer Tools)</li>
</ul>
</div><h3>Breakpoints<span><a class="mark" href="#debugger_breakpoints" id="debugger_breakpoints">#</a></span></h3>
<div class="signature"><ul>
<li><code>setBreakpoint()</code>, <code>sb()</code> - Set breakpoint on current line</li>
<li><code>setBreakpoint(line)</code>, <code>sb(line)</code> - Set breakpoint on specific line</li>
<li><code>setBreakpoint(&#39;fn()&#39;)</code>, <code>sb(...)</code> - Set breakpoint on a first statement in
functions body</li>
<li><code>setBreakpoint(&#39;script.js&#39;, 1)</code>, <code>sb(...)</code> - Set breakpoint on first line of
script.js</li>
<li><code>clearBreakpoint(&#39;script.js&#39;, 1)</code>, <code>cb(...)</code> - Clear breakpoint in script.js
on line 1</li>
</ul>
</div><p>It is also possible to set a breakpoint in a file (module) that
is not loaded yet:</p>
<pre><code class="lang-txt">$ node inspect main.js
&lt; Debugger listening on ws://127.0.0.1:9229/4e3db158-9791-4274-8909-914f7facf3bd
&lt; For help see https://nodejs.org/en/docs/inspector
&lt; Debugger attached.
Break on start in main.js:1
&gt; 1 (function (exports, require, module, __filename, __dirname) { const mod = require(&#39;./mod.js&#39;);
  2 mod.hello();
  3 mod.hello();
debug&gt; setBreakpoint(&#39;mod.js&#39;, 22)
Warning: script &#39;mod.js&#39; was not loaded yet.
debug&gt; c
break in mod.js:22
 20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
 21
&gt;22 exports.hello = function() {
 23   return &#39;hello from module&#39;;
 24 };
debug&gt;
</code></pre>
<h3>Information<span><a class="mark" href="#debugger_information" id="debugger_information">#</a></span></h3>
<div class="signature"><ul>
<li><code>backtrace</code>, <code>bt</code> - Print backtrace of current execution frame</li>
<li><code>list(5)</code> - List scripts source code with 5 line context (5 lines before and
after)</li>
<li><code>watch(expr)</code> - Add expression to watch list</li>
<li><code>unwatch(expr)</code> - Remove expression from watch list</li>
<li><code>watchers</code> - List all watchers and their values (automatically listed on each
breakpoint)</li>
<li><code>repl</code> - Open debugger&#39;s repl for evaluation in debugging script&#39;s context</li>
<li><code>exec expr</code> - Execute an expression in debugging script&#39;s context</li>
</ul>
</div><h3>Execution control<span><a class="mark" href="#debugger_execution_control" id="debugger_execution_control">#</a></span></h3>
<div class="signature"><ul>
<li><code>run</code> - Run script (automatically runs on debugger&#39;s start)</li>
<li><code>restart</code> - Restart script</li>
<li><code>kill</code> - Kill script</li>
</ul>
</div><h3>Various<span><a class="mark" href="#debugger_various" id="debugger_various">#</a></span></h3>
<div class="signature"><ul>
<li><code>scripts</code> - List all loaded scripts</li>
<li><code>version</code> - Display V8&#39;s version</li>
</ul>
</div><h2>Advanced Usage<span><a class="mark" href="#debugger_advanced_usage" id="debugger_advanced_usage">#</a></span></h2>
<h3>V8 Inspector Integration for Node.js<span><a class="mark" href="#debugger_v8_inspector_integration_for_node_js" id="debugger_v8_inspector_integration_for_node_js">#</a></span></h3>
<p>V8 Inspector integration allows attaching Chrome DevTools to Node.js
instances for debugging and profiling. It uses the <a href="https://chromedevtools.github.io/debugger-protocol-viewer/">Chrome Debugging Protocol</a>.</p>
<p>V8 Inspector can be enabled by passing the <code>--inspect</code> flag when starting a
Node.js application. It is also possible to supply a custom port with that flag,
e.g. <code>--inspect=9222</code> will accept DevTools connections on port 9222.</p>
<p>To break on the first line of the application code, pass the <code>--inspect-brk</code>
flag instead of <code>--inspect</code>.</p>
<pre><code class="lang-txt">$ node --inspect index.js
Debugger listening on 127.0.0.1:9229.
To start debugging, open the following URL in Chrome:
    chrome-devtools://devtools/bundled/inspector.html?experiments=true&amp;v8only=true&amp;ws=127.0.0.1:9229/dc9010dd-f8b8-4ac5-a510-c1a114ec7d29
</code></pre>
<p>(In the example above, the UUID dc9010dd-f8b8-4ac5-a510-c1a114ec7d29
at the end of the URL is generated on the fly, it varies in different
debugging sessions.)</p>

      </div>
    </div>
  </div>
  <script src="assets/sh_main.js"></script>
  <script src="assets/sh_javascript.min.js"></script>
  <script>highlight(undefined, undefined, 'pre');</script>
  <!-- __TRACKING__ -->
</body>
</html>

